package tests;

import common.Utils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

public class HotelShop {
    private WebDriver driver;
    //存储登录后的Cookie
    private static Set<Cookie> loginCookies;

    // 每个测试方法前初始化驱动打开浏览器
    @BeforeEach
    void setUp() {
        driver = Utils.initDriver("chrome");
        driver.get("http://localhost:8080/jiudiankefang/jsp/login.jsp");


            login();//首次登陆
    }

    private void login() {
        try {
            WebElement username =Utils.waitForElementVisible(By.cssSelector("#username"),10);
            WebElement password =Utils.waitForElementVisible(By.cssSelector("#loginForm > input:nth-child(2)"),10);
            WebElement submit =Utils.waitForElementClickable(By.cssSelector("#loginForm > button"),10);
            WebElement select =Utils.waitForElementClickable(By.cssSelector("#submitBtn > div:nth-child(2) > label"),10);
            username.sendKeys("111");
            password.sendKeys("111");
            select.click();
            submit.click();
            Utils.pop("登录成功","正常登录测试：弹窗文本不符合预期");
            // 等待标题包含目标文本
            WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
            wait.until(ExpectedConditions.titleContains("入口页"));
            assertTrue(driver.getTitle().contains("入口页"), "正常登录后页面标题不符合预期");
            loginCookies=driver.manage().getCookies();//保存所有Cookie
        }catch (Exception e){
            try {
                Utils.getScreenShot("登录后出现异常");
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
            fail("登录后出现异常：" + e.getMessage());
        }
    }

    @AfterEach
    void teardown() {
        Utils.quitDriver();
    }

    private void openInformationPage(){
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        // 等待遮挡元素消失
        wait.until(ExpectedConditions.invisibilityOfElementLocated(By.className("preloader-it")));

        //一级标题
        WebElement personalCenter =Utils.waitForElementClickable(By.cssSelector("#side-menu > li:nth-child(4) > a"),10);
        personalCenter.click();
        WebElement changePassword =Utils.waitForElementClickable(By.cssSelector("#app_m1 > li:nth-child(2) > a"),10);
        changePassword.click();

        // 1. 定位 iframe 元素
        WebElement iframe = driver.findElement(By.id("mainIframe"));

        // 2. 切换到 iframe 内部
        driver.switchTo().frame(iframe);
    }

    @Test
    public void queryTest(){
        try {
            openInformationPage();
            WebElement searchBox =Utils.waitForElementClickable(By.cssSelector("#nameSearch"),10);
            searchBox.sendKeys("零食大礼包");
            WebElement searchBtn =Utils.waitForElementClickable(By.cssSelector("#content > div:nth-child(3) > div > div > div > div > div.col-sm-12 > button"),10);
            searchBtn.click();
            Utils.waitForElementClickable(By.cssSelector("#tableId > tbody > tr > td:nth-child(2)"),10);
        }catch (Exception e){
            try {
                Utils.getScreenShot("搜索商品出现异常");
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
            fail("搜索商品出现异常：" + e.getMessage());
        }
    }

    //未入住 正确购买
    @Test
    public void onShow(){
        try {
            openInformationPage();
            WebElement BuyBtn =Utils.waitForElementClickable(By.cssSelector("#tableId > tbody > tr:nth-child(1) > td:nth-child(5) > button"),10);
            BuyBtn.click();
            WebElement receive =Utils.waitForElementClickable(By.cssSelector("#receive"),10);
            receive.click();
            Utils.pop("真的要购买 1 件商品吗？","购买商品：弹窗文本不符合预期");
            Utils.pop("只有入住后才可以购买商品哦","只有入住才可以购买商品：弹窗文本不符合预期");

        }catch (Exception e){
            try {
                Utils.getScreenShot("未入住正确购买出现异常");
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
            fail("未入住正确购买出现异常：" + e.getMessage());
        }
    }
    //输入负数商品数量
    @Test
    public void negativeBuy(){
        try {
            openInformationPage();
            WebElement BuyBtn =Utils.waitForElementClickable(By.cssSelector("#tableId > tbody > tr:nth-child(1) > td:nth-child(5) > button"),10);
            BuyBtn.click();
            WebElement numBox=Utils.waitForElementClickable(By.cssSelector("#num"),10);
            numBox.clear();
            numBox.sendKeys("-10");
            WebElement receive =Utils.waitForElementClickable(By.cssSelector("#receive"),10);
            receive.click();
            Utils.pop("真的要购买 -10 件商品吗？","购买商品：弹窗文本不符合预期");
            Utils.pop("购买的商品数量不能小于1","购买商品数量测试：弹窗文本不符合预期");

        }catch (Exception e){
            try {
                Utils.getScreenShot("未入住正确购买出现异常");
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
            fail("未入住正确购买出现异常：" + e.getMessage());
        }
    }
}
